<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  .box {
    position: relative;
    margin: 0 auto;
    width: 1200px;
    height: 200px;
    border: 1px solid black;
  }

  span {
    position: absolute;
    text-align: center;
    font-size: 36px;
    top: 0;
    left: 0;
    display: block;
  }
</style>

<body>
  <div class="box">

  </div>
  <button>开始</button>

  <script>
    let box = document.querySelector(".box");
    let body = document.querySelector("body");
    let button = document.querySelector("button")
    let span = [];
    let word = [];
    let space = 50;
    let speed = 1000;
    let newspan = null;
    for (let i = 0; i < 1; i++) {
      span[i] = document.createElement("span");
      word[i] = String.fromCharCode((Math.ceil(Math.random() * 25 + 97)));
      span[i].innerText = word[i];
      box.appendChild(span[i]);
    }

    body.addEventListener("keydown", () => {
      inp = String.fromCharCode(event.keyCode).toLowerCase();
      if (inp == span[0].innerText) {
        span[0].style.display = "none";
        // newspan = document.createElement("span");
        // newspan.innerText = String.fromCharCode((Math.ceil(Math.random() * 25 + 97)));
        // box.appendChild(newspan);
      }
    })
    button.addEventListener("click", () => {
      run();
    })

    function run() {
      // setInterval(() => {
      //   span[0].style.top = space + "px";
      // }, 1000);
      let timer = setTimeout(run, speed);

      span[0].style.top = space + "px";
      speed -= 75;
      space += 50;
      console.log(space);
      if (space == 650) {
        span[0].style.display = "none";
        clearTimeout(timer);
      }


    }


  </script>



</body>

</html>